global root_dir = "`1'"

include "$root_dir/code/config/config.do"

cap noi log using ${log_dir}/Table_A30_shocklevel_summary.log, replace name(tabl)

capture noi { 

    * Table_shocklevel_summary
    log using ${numb_dir}/Table_A30_shocklevel_summary_numbers.log, replace name(numbers)


    texdoc init ${tab_dir}/appendix/Table_A30_shocklevel_summary.tex, replace
    texdoc stlog, nolog

    use ${final_dir}/regression_dataset_from1970_tfacit1_countrylevel_resid.dta, clear

    * A: Summary of ISP variation
    foreach var in ISP rISP_f rISP_fiycy rISP_ssfiycy { 
        su `var', d
        texdoc local `var'_mean : display %10.2f r(mean)
        if (`var' == ISP) texdoc local ISP_mean : display %10.2f r(mean) // overwrite 
        texdoc local `var'_sd : display %10.1f r(sd) * 100
        texdoc local `var'_iqr : display %10.1f (r(p75) - r(p25)) * 100
    }

    cap log close numbers
    
    * output table (via texdoc)
    texdoc local notes "This table reports summary statistics on the log inverse skill premium weighted by the average country weight in our regression sample as in Borusyak, Hull and Jaravel (2022). The log inverse skill premium is residualized on firm fixed effects (Columns 2, 3 and 4), industry-year and country-year fixed effects (Columns 3 and 4) and stocks and spillovers (Column 4)."
    texdoc stlog close
    qui include ${code_dir}/config/texdoctools.do
    tex `texpreamble'

    /*tex
    \begin{tabular}{@{}p{0.18\textwidth}>{\centering}p{0.04\textwidth}>{\centering}p{0.04\textwidth}>{\centering}p{0.04\textwidth}>{\centering\arraybackslash}p{0.04\textwidth}@{}}
    \toprule
    \noalign{\smallskip}
    & (1) & (2) & (3) & (4) \\  
    \midrule

    Mean                      & `ISP_mean' &    0           &   0                   &   0                   \\
    Standard deviation (\%)   & `ISP_sd'   & `rISP_f_sd'    &   `rISP_fiycy_sd'     &   `rISP_ssfiycy_sd'   \\
    Interquartile range (\%)  & `ISP_iqr'  & `rISP_f_iqr'   &   `rISP_fiycy_iqr'    &   `rISP_ssfiycy_iqr'  \\

    Residualizing on \dots & & & & \\
    \enspace F fixed effect 	    & -- & Yes  & Yes & Yes \\
    \enspace IY+CY fixed effects  & -- & --   & Yes & Yes \\
    \enspace Stocks/Spillovers    & -- & --   & --  & Yes \\

    \bottomrule
    \end{tabular}
    tex*/

    tex `texpostamble'
    texdoc close

}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}